import * as types from '../mutation-types';
import {fetch} from 'utils/api.js';
import Tools from 'utils/tools.js';
import {ARTICLE_TYPE, ARTICLE_STATUS} from 'store/constants';

const state = {};

const unCacheState = {
  //上传阶段(是否已选择文件)
  stage: 'before',
  // 分类(视频/音频)
  category: 'video',
  // 类型 (单视频：single 视频集:set)
/*  videoType: ARTICLE_TYPE.SINGLEVIDEO,*/
  // 上传状态(uploading uploaded)
  status: 'standby',
  progress: 0,
  // 是否在提交数据
  pushing: false,
  uploader: null,
  request: null,
  // 是否是修改视频
/*  isEdit: false,*/
  editingVideo: {},
/*  editingVideoSet: {
    id: null,
    title: null,
    description: null,
    imageUrl: null
  }*/
};

const cacheState = {
  options: [],
  hasFetchOptions: false
};
Object.assign(state, unCacheState);

const defaultState = {};
Tools.clone(state, defaultState);

Object.assign(state, cacheState);

const mutations = {
  [types.VIDEO_BEGIN_UPLOAD](state, {uploader, request}) {
    state.uploader = uploader;
    state.request = request;
    state.status = 'uploading';
    state.category = 'video';
    state.stage = 'after';
    state.progress = 0;
  },
  [types.VIDEO_END_UPLOAD](state, status) {
    state.status = status || 'uploaded';
    state.uploader = null;
    state.request = null;
  },
  [types.VIDEO_SET_TEMP_DATA](state, formData) {
    Object.assign(state.editingVideo, formData);

    if(formData.type == 'video'){
      state.category = 'video';
    }
    if(formData.type == 'audio'){
      state.category = 'audio';
    }
    state.stage = 'after';
  },
/*  [types.VIDEOSET_SET_TEMP_DATA](state, formData) {
    Object.assign(state.editingVideoSet, formData);
  },*/
  [types.MEDIA_CLEAR_TEMP_DATA](state) {
    let def = {};
    Tools.clone(defaultState, def);

    for (let key in state) {
      if (state.hasOwnProperty(key) && def.hasOwnProperty(key)) {
        state[key] = def[key];
      }
    }
  },
  [types.MEDIA_SET_UPLOAD_PROGRESS](state, progress) {
    state.progress = progress;
  },
  // 点击上传音频
  [types.AUDIO_BEGIN_UPLOAD](state,{uploader, request}) {
    state.uploader = uploader;
    state.request = request;
    state.status = 'uploading';
    state.category = 'audio';
    state.stage = 'after';
    state.progress = 0;
  },
/*  [types.VIDEO_SET_TYPE](state, type) {
    state.videoType = type;
  },*/
/*  [types.VIDEO_SET_EDIT_STATUS](state, isEdit) {
    state.isEdit = isEdit;
  },*/
  [types.MEDIA_PUSH_START](state) {
    state.pushing = true;
  },
  [types.MEDIA_PUSH_END](state) {
    state.pushing = false;
  },
  [types.MEDIA_GET_START](state) {
    state.getting = true;
  },
  [types.MEDIA_GET_END](state) {
    state.getting = false;
  },
/*  [types.VIDEOSET_SET_OPTIONS](state, options) {
    state.options = options;
    state.hasFetchOptions = true;
  }*/
};

const actions = {
  audioBeginUpload({commit}, {uploader, request}) {
    commit(types.AUDIO_BEGIN_UPLOAD, {uploader, request});
  },
  videoBeginUpload({commit}, {uploader, request}) {
    commit(types.VIDEO_BEGIN_UPLOAD, {uploader, request});
  },
  videoEndUpload({commit}, status) {
    commit(types.VIDEO_END_UPLOAD, status);
  },
  videoSetTempData({commit}, formData) {
    commit(types.VIDEO_SET_TEMP_DATA, formData);
  },
/*  videoSetSetTempData({commit}, formData) {
    commit(types.VIDEOSET_SET_TEMP_DATA, formData);
  },*/
/*  videoSetType({commit}, type) {
    commit(types.VIDEO_SET_TYPE, type);
  },
  videoSetEditStatus({commit}, isEdit) {
    commit(types.VIDEO_SET_EDIT_STATUS, isEdit);
  },*/
  videoSubmit({commit, dispatch, state}) {
    if (!state.pushing) {
      commit(types.MEDIA_PUSH_START);
      let pushData = {};
      Tools.clone(state.editingVideo, pushData);
      
      return new Promise((resolve, reject) => {
        fetch('saveVideo', {body: pushData}).then((res) => {
          if (res.code === 200) {
            resolve();
            dispatch('mediaClearTempData');
          }
          else {
            reject(res);
          }
          commit(types.MEDIA_PUSH_END);
        }, (res) => {
          reject(res);
          commit(types.MEDIA_PUSH_END);
          console.log(res);
        });
      });
      
    }
  },
  audioSubmit({commit, dispatch, state}) {
    if (!state.pushing) {
      commit(types.MEDIA_PUSH_START);
      let pushData = {};
      Tools.clone(state.editingVideo, pushData);
      
      return new Promise((resolve, reject) => {
        fetch('saveAudio', {body: pushData}).then((res) => {
          if (res.code === 200) {
            resolve();
            dispatch('mediaClearTempData');
          }
          else {
            reject(res);
          }
          commit(types.MEDIA_PUSH_END);
        }, (res) => {
          reject(res);
          commit(types.MEDIA_PUSH_END);
          console.log(res);
        });
      });
      
    }
  },
/*  videoSetSubmit({commit, dispatch}) {
    if (!state.pushing) {
      commit(types.MEDIA_PUSH_START);
      let pushData = Tools.clone(state.editingVideoSet);
      
      return new Promise((resolve, reject) => {
        fetch('savePlayInfo', {body: pushData}).then((res) => {
          if (res.code === 200) {
            resolve();
            dispatch('mediaClearTempData');
          }
          else {
            reject(res);
          }
          commit(types.MEDIA_PUSH_END);
        }, (res) => {
          reject(res);
          commit(types.MEDIA_PUSH_END);
          console.log(res);
        });
      });
      
    }
  },*/
  mediaClearTempData({commit}) {
    commit(types.MEDIA_CLEAR_TEMP_DATA);
  },
  videoLoadInfo({commit}, contentId) {
    return new Promise((resolve, reject) => {
      commit(types.MEDIA_GET_START);
      fetch('videoInfo', {body: {id:contentId}}).then((res) => {
        commit(types.MEDIA_GET_END);
        resolve(res);
      }, (res) => {
        commit(types.MEDIA_GET_END);
        reject(res);
      });
    });
  },
  audioLoadInfo({commit}, contentId) {
    return new Promise((resolve, reject) => {
      commit(types.MEDIA_GET_START);
      fetch('audioInfo', {body: {id:contentId}}).then((res) => {
        commit(types.MEDIA_GET_END);
        resolve(res);
      }, (res) => {
        commit(types.MEDIA_GET_END);
        reject(res);
      });
    });
  },
  videoSetLoadInfo({commit}, contentId) {
    return new Promise((resolve, reject) => {
      commit(types.MEDIA_GET_START);
      fetch('getPlayDetail', {body: {contentId}}).then((res) => {
        commit(types.MEDIA_GET_END);
        resolve(res);
      }, (res) => {
        commit(types.MEDIA_GET_END);
        reject(res);
      });
    });
  },
  mediaSetUploadProgress({commit}, progress) {
    commit(types.MEDIA_SET_UPLOAD_PROGRESS, progress);
  },
  // 设置上传到视频集
/*  videoSetTypeSet({commit, dispatch}) {
    let fetchList = fetch('videoSetList', {
      url: {
        status: ARTICLE_STATUS.PUBLISHED
      },
      body: {
        page: 1,
        size: 9999
      }
    });
    fetchList.then((res) => {
      if (res.code !== 200)
        return dispatch('alert', '获取合集失败');
      
      let list = res.data.item;
      if (list.length === 0){
        return
      }
      
      commit(types.VIDEOSET_SET_OPTIONS, list.map((videoSet) => {
        return {
          playContentId: videoSet.id,
          title: videoSet.title,
          pid: videoSet.plid
        };
      }));
      commit(types.VIDEO_SET_TYPE, ARTICLE_TYPE.VIDEOSET);
    }).catch(() => dispatch('alert', '获取合集失败'));
    return fetchList;
  }*/
};

const getters = {
  publishMediaChild(state) {
    let stage = state.stage;
    let category = state.category;

    if (stage == 'before')
      return 'before-media-publish';
    if (stage == 'after') {
      return `after-${category}-uploaded`;
    }
  },
  hasTempVideoDate(state) {
    for (let key in state.editingVideo) {
      if (state.editingVideo.hasOwnProperty(key)) {
        let val = state.editingVideo[key];
        if (!(val === null || val === undefined || val === '' || val.length === 0)) {
          return true;
        }
      }
    }
    return false;
  },
/*  hasTempVideoSetDate(state) {
    for (let key in state.editingVideoSet) {
      if (state.editingVideoSet.hasOwnProperty(key)) {
        let val = state.editingVideoSet[key];
        if (!(val === null || val === undefined || val === '' || val.length === 0)) {
          return true;
        }
      }
    }
    return false;
  }*/
};

export default {
  state,
  mutations,
  getters,
  actions
};
